package com.dms.servlet;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dms.bean.Announce;
import com.dms.bean.Student;
import com.dms.dao.AnnounceDao;
import com.dms.model.AnnounceModel;

/**
 * Servlet implementation class AnnounceServlet
 */
@WebServlet("/AnnounceServlet")
public class AnnounceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AnnounceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		 response.setCharacterEncoding("UTF-8");
	   	 request.setCharacterEncoding("UTF-8");
	   	HttpSession session = request.getSession();
		Student stu = (Student) session.getAttribute("student");
	   	String action = request.getParameter("act");
		if (action == null)
			action = "";
		if (action.equals("add")) {
			this.add(request, response,stu);
		}
		if (action.equals("list")) {
			this.list(request, response,stu);
		}
		if (action.equals("edit")) {
			this.edit(request, response,stu);
		}
		if (action.equals("update")) {
			this.update(request, response,stu);
		}
		if (action.equals("info")) {
			this.info(request, response,stu);
		}
		if (action.equals("delete")) {
			this.delete(request, response,stu);
		}
	}
	
protected void add(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		
		String content = request.getParameter("content");
		String title = request.getParameter("title");
		Timestamp date = new Timestamp(System.currentTimeMillis()); 
		if (content == null || "".equals(content) || title == null || "".equals(title)) {
			request.setAttribute("message", "天哪噜,缺数据或数据错误(⊙o⊙)…");
			request.getRequestDispatcher("addAnnounce.jsp").forward(request, response);
		}
		boolean val = AnnounceModel.add(content, title, date);
		if (!val) {
			request.setAttribute("message", "天哪噜,失败啦(⊙o⊙)…");
			request.getRequestDispatcher("addAnnounce.jsp").forward(request, response);
		} else {
			response.sendRedirect("AnnounceServlet?act=list");
		}
	}
	protected void list(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		List<Announce> announces = AnnounceDao.list();
		request.setAttribute("list", announces);
		request.getRequestDispatcher("announce.jsp").forward(request, response);
	}
	protected void edit(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		int annID = Integer.parseInt(request.getParameter("annID"));
		Announce announce =  AnnounceDao.find(annID);
		request.setAttribute("announce", announce);
		request.getRequestDispatcher("editAnnounce.jsp").forward(request, response);
	}
	protected void update(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		String content = request.getParameter("content");
		String title = request.getParameter("title");
		Timestamp date = new Timestamp(System.currentTimeMillis()); 
		if (content == null || "".equals(content) || title == null || "".equals(title)) {
			request.setAttribute("message", "天哪噜,缺数据或数据错误(⊙o⊙)…");
			request.getRequestDispatcher("addAnnounce.jsp").forward(request, response);
		}
		int annID = Integer.parseInt(request.getParameter("annID"));
		Announce announce = new Announce();
		announce.setAnnID(annID);
		announce.setContent(content);
		announce.setTitle(title);
		announce.setDate(date);
		boolean var =false;
		try {
		      var =  AnnounceDao.save(announce);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!var){
			request.setAttribute("message", "修改失败 @_@");
			request.getRequestDispatcher("editAnnounce.jsp").forward(request, response);
		}else{
			response.sendRedirect("AnnounceServlet?act=list");
		}
	}
	protected void info(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("annID"));
		Announce announce = AnnounceDao.find(id);
		request.setAttribute("announce", announce);
		request.getRequestDispatcher("announceInfo.jsp").forward(request, response);
	}
	protected void delete(HttpServletRequest request, HttpServletResponse response,Student stu) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("annID"));
		boolean var =false;
		try {
		      var =  AnnounceDao.delete(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!var){
			request.setAttribute("message", "删除失败 @_@");
			request.getRequestDispatcher("editAnnounce.jsp").forward(request, response);
		}else{
			response.sendRedirect("AnnounceServlet?act=list");
		}
	}

}
